#include "lv_user_ui.h"
#include "lv_user.h"
#include "app.h"
#include "io.h"


const char * ui_calib_sel_text[3][6]=
{
    {
        "Parameter calib",
        "Input voltage calib",
        "Input current calib",
        "Output voltage calib",
        "Output current calib",
        "Exit",
    },
    {
        "参数校准",
        "输入电压校准",
        "输入电流校准",
        "输出电压校准",
        "输出电流校准",
        "退出",
    },
    {
        "參數校準",
        "輸入電壓校準",
        "輸入電流校準",
        "輸出電壓校準",
        "輸出電流校準",
        "退出",
    },
};

const char * ui_calib_btn_text[3][3]=
{
    {
        "Calib\nParam1",
        "Calib\nParam2",
        "Exit",
    },
    {
        "校准参数1",
        "校准参数2",
        "退出",
    },
    {
        "校準參數1",
        "校準參數2",
        "退出",
    },
};

ui_calib_sel_page calib_sel_page;

static void ui_event_cb(lv_event_t * e)
{
    uint8_t i;
    lv_obj_t * obj = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_SHORT_CLICKED)
    {
        //退出校准选择
        if(obj == calib_sel_page.btn_cancel)
        {
            ui_status.calib_create_flag = 0;
            lv_obj_del(calib_sel_page.main_cont);
        }
        //选择输入电压校准
        else if(obj == calib_sel_page.btn_calib[0])
        {
            if(ui_status.calib_create_flag == 1)
            {
                ui_status.calib_create_flag = 0;
                lv_obj_del(calib_sel_page.main_cont);
                
                calib_ui_init(&calib_obj_input_vol, lv_scr_act());
            }
        }
        else if(obj == calib_sel_page.btn_calib[1])
        {
            if(ui_status.calib_create_flag == 1)
            {
                ui_status.calib_create_flag = 0;
                lv_obj_del(calib_sel_page.main_cont);
                
                calib_ui_init(&calib_obj_input_cur, lv_scr_act());
            }
        }
        else if(obj == calib_sel_page.btn_calib[2])
        {
            if(ui_status.calib_create_flag == 1)
            {
                ui_status.calib_create_flag = 0;
                lv_obj_del(calib_sel_page.main_cont);
                
                calib_ui_init(&calib_obj_output_vol, lv_scr_act());
            }
        }
        else if(obj == calib_sel_page.btn_calib[3])
        {
            if(ui_status.calib_create_flag == 1)
            {
                ui_status.calib_create_flag = 0;
                lv_obj_del(calib_sel_page.main_cont);
                
                calib_ui_init(&calib_obj_output_cur, lv_scr_act());
            }
        }
        else if(obj == calib_page.btn_para1)
        {
        }
        else if(obj == calib_page.btn_para2)
        {
        }
        //退出校准
        else if(obj == calib_page.btn_exit)
        {
            lv_obj_del(calib_page.main_cont);
        }
    }
}

void calib_sel_ui_init(lv_obj_t * parent)
{
    int i;
    calib_sel_page.main_cont = lv_obj_create(parent);
    lv_obj_set_style_pad_all(calib_sel_page.main_cont, 0, 0);
    lv_obj_set_style_pad_gap(calib_sel_page.main_cont, 0, 0);
    lv_obj_set_style_radius(calib_sel_page.main_cont, 5, 0);
    //lv_obj_clear_flag(calib_sel_page.main_cont, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_size(calib_sel_page.main_cont, 280, 200);
    lv_obj_align(calib_sel_page.main_cont, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_border_width(calib_sel_page.main_cont, 2, 0);
    lv_obj_set_style_border_color(calib_sel_page.main_cont, lv_color_black(), 0);
    lv_obj_set_style_bg_color(calib_sel_page.main_cont, lv_palette_lighten(LV_PALETTE_BLUE, 2), 0);
    
    calib_sel_page.label_title = lv_label_create(calib_sel_page.main_cont);
    lv_obj_set_style_text_font(calib_sel_page.label_title, &font_siyuanheitim_16, 0);
    lv_label_set_text(calib_sel_page.label_title, ui_calib_sel_text[sys_cur_para.user_set.language%3][0]);
    lv_obj_align(calib_sel_page.label_title, LV_ALIGN_TOP_MID, 0, 0);
    
    lv_obj_t * label;
    for(i=0;i<4;i++)
    {
        calib_sel_page.btn_calib[i] = lv_btn_create(calib_sel_page.main_cont);
        
        label = lv_label_create(calib_sel_page.btn_calib[i]);
        lv_obj_set_style_text_font(label, &font_song_12, 0);
        lv_label_set_text(label, ui_calib_sel_text[sys_cur_para.user_set.language][1+i]);
        
        if(i == 0)
            lv_obj_align_to(calib_sel_page.btn_calib[i], calib_sel_page.label_title, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
        else
            lv_obj_align_to(calib_sel_page.btn_calib[i], calib_sel_page.btn_calib[i-1], LV_ALIGN_OUT_BOTTOM_LEFT, 0, 6);
        lv_obj_add_event_cb(calib_sel_page.btn_calib[i], ui_event_cb, LV_EVENT_ALL, NULL);
    }
    
    calib_sel_page.btn_cancel = lv_btn_create(calib_sel_page.main_cont);
    lv_obj_set_style_text_font(calib_sel_page.btn_cancel, &font_song_12, 0);
    label = lv_label_create( calib_sel_page.btn_cancel);
    lv_obj_set_style_text_font(label, &font_song_12, 0);
    lv_label_set_text(label, ui_calib_sel_text[sys_cur_para.user_set.language][5]);
    lv_obj_align_to(calib_sel_page.btn_cancel, calib_sel_page.btn_calib[3], LV_ALIGN_OUT_BOTTOM_MID, 0, 6);
    lv_obj_add_event_cb(calib_sel_page.btn_cancel, ui_event_cb, LV_EVENT_ALL, NULL);
    
    lv_obj_add_state(calib_sel_page.btn_calib[0], LV_STATE_FOCUS_KEY);
}

calib_obj calib_obj_input_vol =
{
    .main_title = "输入电压校准",
    .main_expl = "请输入2个压差尽量大的电压并对照万用表输入实际电压以校准输入电压读取adc",
    
    .title_para_mes1 = "电压1(低压)原始数据(ADC原始数据):",
    .title_para_real1 = "电压1(低压)真实数据(万用表读数mV):",
    .title_para_mes2 = "电压2(高压)原始数据(ADC原始数据):",
    .title_para_real2 = "电压2(高压)真实数据(万用表读数mV):",
    .val_mes1 = 0,
    .val_mes2 = 0,
    .val_real1 = 0,
    .val_real2 = 0,
    .calib_out1 = 0,
    .calib_out2 = 0,
};

calib_obj calib_obj_input_cur =
{
    .main_title = "输入电流校准",
    .main_expl = "请调节电源输出并连接负载以输出2个电流差尽量大的电压并对照万用表输入实际电流以校准输入电流读取adc",
    
    .title_para_mes1 = "电流1(低流)原始数据(ADC原始数据):",
    .title_para_real1 = "电流1(低流)真实数据(万用表读数mA):",
    .title_para_mes2 = "电流2(高流)原始数据(ADC原始数据):",
    .title_para_real2 = "电流2(高流)真实数据(万用表读数mA):",
    .val_mes1 = 0,
    .val_mes2 = 0,
    .val_real1 = 0,
    .val_real2 = 0,
    .calib_out1 = 0,
    .calib_out2 = 0,
};

calib_obj calib_obj_output_vol =
{
    .main_title = "输出电压校准",
    .main_expl = "请输出2个压差尽量大的电压并对照万用表输入实际电压以校准输出电压读取adc",
    
    .title_para_mes1 = "电压1(低压)原始数据(ADC原始数据):",
    .title_para_real1 = "电压1(低压)真实数据(万用表读数mV):",
    .title_para_mes2 = "电压2(高压)原始数据(ADC原始数据):",
    .title_para_real2 = "电压2(高压)真实数据(万用表读数mV):",
    .val_mes1 = 0,
    .val_mes2 = 0,
    .val_real1 = 0,
    .val_real2 = 0,
    .calib_out1 = 0,
    .calib_out2 = 0,
};

calib_obj calib_obj_output_cur =
{
    .main_title = "输出电流校准",
    .main_expl = "请调节电源输出并连接负载以输出2个电流差尽量大的电压并对照万用表输入实际电流以校准输出电流读取adc",
    
    .title_para_mes1 = "电流1(低流)原始数据(ADC原始数据):",
    .title_para_real1 = "电流1(低流)真实数据(万用表读数mA):",
    .title_para_mes2 = "电流2(高流)原始数据(ADC原始数据):",
    .title_para_real2 = "电流2(高流)真实数据(万用表读数mA):",
    .val_mes1 = 0,
    .val_mes2 = 0,
    .val_real1 = 0,
    .val_real2 = 0,
    .calib_out1 = 0,
    .calib_out2 = 0,
};

ui_calib_page calib_page;

void calib_ui_init(calib_obj *calib_main, lv_obj_t * parent)
{
    calib_page.main_cont = lv_obj_create(parent);
    lv_obj_set_style_pad_all(calib_page.main_cont, 0, 0);
    lv_obj_set_style_pad_gap(calib_page.main_cont, 0, 0);
    lv_obj_set_style_radius(calib_page.main_cont, 5, 0);
    lv_obj_clear_flag(calib_page.main_cont, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_size(calib_page.main_cont, 280, 200);
    lv_obj_align(calib_page.main_cont, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_border_width(calib_page.main_cont, 2, 0);
    lv_obj_set_style_border_color(calib_page.main_cont, lv_color_black(), 0);
    lv_obj_set_style_bg_color(calib_page.main_cont, lv_palette_lighten(LV_PALETTE_BLUE, 2), 0);
    
    calib_page.label_title = lv_label_create(calib_page.main_cont);
    lv_obj_set_style_text_font(calib_page.label_title, &font_siyuanheitim_16, 0);
    lv_label_set_text(calib_page.label_title, calib_main->main_title);
    lv_obj_align(calib_page.label_title, LV_ALIGN_TOP_MID, 0, 0);
    
    calib_page.label_expl = lv_label_create(calib_page.main_cont);
    lv_obj_set_style_text_font(calib_page.label_expl, &font_song_12, 0);
    lv_label_set_text(calib_page.label_expl, calib_main->main_expl);
    lv_label_set_long_mode(calib_page.label_expl, LV_LABEL_LONG_SCROLL);
    lv_obj_align_to(calib_page.label_expl, calib_page.label_title, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
    
    lv_obj_t * label;
    
    calib_page.btn_para1 = lv_btn_create(calib_page.main_cont);
    label = lv_label_create(calib_page.btn_para1);
    lv_obj_set_style_text_font(label, &font_song_12, 0);
    lv_label_set_text(label, ui_calib_btn_text[sys_cur_para.user_set.language][0]);
    lv_obj_center(label);
    lv_obj_align(calib_page.btn_para1, LV_ALIGN_BOTTOM_LEFT, 2, -2);
    
    calib_page.btn_para2 = lv_btn_create(calib_page.main_cont);
    label = lv_label_create(calib_page.btn_para2);
    lv_obj_set_style_text_font(label, &font_song_12, 0);
    lv_label_set_text(label, ui_calib_btn_text[sys_cur_para.user_set.language][1]);
    lv_obj_center(label);
    lv_obj_align_to(calib_page.btn_para2, calib_page.btn_para1, LV_ALIGN_OUT_RIGHT_MID, 5, 0);
    
    calib_page.btn_exit = lv_btn_create(calib_page.main_cont);
    label = lv_label_create(calib_page.btn_exit);
    lv_obj_set_style_text_font(label, &font_song_12, 0);
    lv_label_set_text(label, ui_calib_btn_text[sys_cur_para.user_set.language][2]);
    lv_obj_center(label);
    lv_obj_align_to(calib_page.btn_exit, calib_page.btn_para2, LV_ALIGN_OUT_RIGHT_MID, 5, 0);
    
    lv_obj_add_event_cb(calib_page.btn_para1, ui_event_cb, LV_EVENT_ALL, NULL);
    lv_obj_add_event_cb(calib_page.btn_para2, ui_event_cb, LV_EVENT_ALL, NULL);
    lv_obj_add_event_cb(calib_page.btn_exit, ui_event_cb, LV_EVENT_ALL, NULL);
    
    lv_obj_add_state(calib_page.btn_para1, LV_STATE_FOCUS_KEY);
}
